package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.silkroad.UserSilkRoadData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserSilkRoadDaoImpl extends TempDataObjectDao<UserSilkRoadData> {
    public static String getUserSilkRoadDataSql = "select * from `t_u_silkroad` where `userId`=? and `activityId`=?";

    public UserSilkRoadData getUserSilkRoadData(long userId, int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserSilkRoadDataSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserSilkRoadDataSql = "update `t_u_silkroad` set routeIndex=?,achievement=?,score=?,eventIds=?,refloatItemMap=?,nowSea=?,mapId=?,remainSeaItemMap=?,seaCount=? where userId=? and activityId=?";
    @Override
    protected boolean updateData(UserSilkRoadData userSilkRoadData) {
        userSilkRoadData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userSilkRoadData.getRouteIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, userSilkRoadData.getAchievement()));
        para.put(i++, new DbParameter(Types.BIGINT, userSilkRoadData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userSilkRoadData.getEventIds())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userSilkRoadData.getRefloatItemMap())));
        para.put(i++, new DbParameter(Types.INTEGER, userSilkRoadData.getNowSea()));
        para.put(i++, new DbParameter(Types.INTEGER, userSilkRoadData.getMapId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userSilkRoadData.getRemainSeaItemMap())));
        para.put(i++, new DbParameter(Types.INTEGER, userSilkRoadData.getSeaCount()));
        para.put(i++, new DbParameter(Types.BIGINT, userSilkRoadData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userSilkRoadData.getActivityId()));
        if (getDb().executeNoneQuery(updateUserSilkRoadDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserSilkRoadDataSql = "insert into `t_u_silkroad`(userId,activityId,routeIndex,achievement,score,eventIds,refloatItemMap,nowSea,mapId,remainSeaItemMap,seaCount) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserSilkRoadData userSilkRoadData) {
        userSilkRoadData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userSilkRoadData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userSilkRoadData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, userSilkRoadData.getRouteIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, userSilkRoadData.getAchievement()));
        para.put(i++, new DbParameter(Types.BIGINT, userSilkRoadData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userSilkRoadData.getEventIds())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userSilkRoadData.getRefloatItemMap())));
        para.put(i++, new DbParameter(Types.INTEGER, userSilkRoadData.getNowSea()));
        para.put(i++, new DbParameter(Types.INTEGER, userSilkRoadData.getMapId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userSilkRoadData.getRemainSeaItemMap())));
        para.put(i++, new DbParameter(Types.INTEGER, userSilkRoadData.getSeaCount()));
        if (getDb().executeNoneQuery(insertUserSilkRoadDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserSilkRoadData parseData(ResultSet rs) throws SQLException {
        UserSilkRoadData userSilkRoadData = new UserSilkRoadData();
        userSilkRoadData.setUserId(rs.getLong("userId"));
        userSilkRoadData.setActivityId(rs.getInt("activityId"));
        userSilkRoadData.setRouteIndex(rs.getInt("routeIndex"));
        userSilkRoadData.setAchievement(rs.getInt("achievement"));
        userSilkRoadData.setScore(rs.getLong("score"));
        userSilkRoadData.setEventIds(JSON.parseObject(rs.getString("eventIds"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        userSilkRoadData.setRefloatItemMap(JSON.parseObject(rs.getString("refloatItemMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        userSilkRoadData.setNowSea(rs.getInt("nowSea"));
        userSilkRoadData.setMapId(rs.getInt("mapId"));
        userSilkRoadData.setRemainSeaItemMap(JSON.parseObject(rs.getString("remainSeaItemMap"), new TypeReference<java.util.Map<java.lang.Integer, java.util.Set<java.lang.Integer>>>() {}));
        userSilkRoadData.setSeaCount(rs.getInt("seaCount"));

        return userSilkRoadData;
    }
}
